<template>
	<view class="userInfo">
		<!-- tabbar-个人中心页 -->
		<view class="info">
			<image :src="avatar" mode="scaleToFill" @click="previewImage"></image>
			<text class="username">{{username}}</text>
			<text @click="goUserdetail">></text>
		</view>
		
		<view class="order" @click="goOrder">
			<text>订 单 列 表</text>
			<text>></text>
		</view>
		
		<view class="btns">
			<button
			type="primary"
			@click="goUpdatapasswd"
			>修 改 密 码</button>
			<button
			type="primary"
			@click="goMyadvise"
			>意 见 反 馈</button>
		</view>
		
		<button
		type="primary"
		@click="quit"
		>退 出 登 录</button>
	</view>
</template>

<script>
	import { http } from "../../utils/request.js"
	export default {
		data() {
			return {
				username:"",
				avatar:""
			}
		},
		onLoad(){
			// 登录判断
			if(uni.getStorageSync("token")==""||uni.getStorageSync("username")==""){
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			// 从缓存中获取当前要登录的用户名（仅提示作用）
			this.username=uni.getStorageSync("username")||"账户"
			this.getAvatar()
		},
		methods: {
			// 获取用户头像
			getAvatar(){
				http({
					url:"/prod-api/api/common/user/getInfo"
				}).then(res=>{
					if(res.data.code == 200){
						// 用户没有设置头像就用logo图代替
						this.avatar=res.data.user.avatar||"/static/logo.png"
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
				})
			},
			// 用户头像点击预览
			previewImage(){
				let imgsArray = [];
				imgsArray[0] = this.avatar;
				uni.previewImage({
					current: 0,
					urls: imgsArray
				})
			},
			// 跳转到个人信息页
			goUserdetail(){
				uni.navigateTo({
					url:"/pages/userDetail/userDetail"
				})
			},
			// 跳转到订单列表页
			goOrder(){
				uni.showToast({
					title:"订单列表（待补充）",
					icon:"none"
				})
			},
			// 跳转到修改密码页
			goUpdatapasswd(){
				uni.navigateTo({
					url:"/pages/updataPasswd/updataPasswd"
				})
			},
			// 跳转到意见反馈页
			goMyadvise(){
				uni.navigateTo({
					url:"/pages/myAdvise/myAdvise"
				})
			},
			// 退出登录
			quit(){
				uni.removeStorageSync("token"),
				uni.removeStorageSync("username"),
				uni.redirectTo({
					url:"/pages/login/login"
				})
				setTimeout(()=>{
					uni.showToast({
						title:"退出成功！"
					})
				},100)
			}
		}
	}
</script>

<style lang="scss">
.userInfo{
	padding: 40rpx;
	.info{
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		width: 100%;
		height: 200rpx;
		padding: 25rpx;
		background-color: #007aff;
		border-radius: 20rpx;
		color: #fff;
		image{
			width: 150rpx;
			height: 150rpx;
			background-color: #f8f8f8;
			border: 1rpx solid #f8f8f8;
			border-radius: 20rpx;
		}
		text{
			font-size: 60rpx;
		}
		.username{
			flex: 1;
			margin: 0 0 100rpx 35rpx;
			font-size: 50rpx;
		}
	}
	.order{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx 0 60rpx;
		height: 110rpx;
		margin-top: 40rpx;
		background-color: #007aff;
		border-radius: 20rpx;
		color: #fff;
		font-size: 38rpx;
	}
	button{
		margin-top: 40rpx;
		border-radius: 20rpx;
		font-size: 38rpx;
	}
	button::after{
		border: 0;
	}
	.btns{
		display: flex;
		justify-content: space-between;
		margin-top: 40rpx;
		button{
			width: 47%;
			margin: 0;
		}
	}
}
</style>
